class Leaderboard:
    def __init__(self):
        self.leaderboard_data = {}

    def add_score(self, player_name, score):
        if player_name in self.leaderboard_data:
            self.leaderboard_data[player_name] += score
        else:
            self.leaderboard_data[player_name] = score

    def get_top_scores(self, num_scores):
        sorted_leaderboard = sorted(self.leaderboard_data.items(), key=lambda x: x[1], reverse=True)
        return sorted_leaderboard[:num_scores]

    def get_player_rank(self, player_name):
        sorted_leaderboard = sorted(self.leaderboard_data.items(), key=lambda x: x[1], reverse=True)
        player_rank = [x[0] for x in sorted_leaderboard].index(player_name) + 1
        return player_rank


# 创建排行榜对象
leaderboard = Leaderboard()

# 添加分数
leaderboard.add_score("Alice", 100)
leaderboard.add_score("Bob", 85)
leaderboard.add_score("Charlie", 120)
leaderboard.add_score("David", 95)

# 获取前两名玩家和排名
top_scores = leaderboard.get_top_scores(2)
print("Top 2 Scores:", top_scores)

alice_rank = leaderboard.get_player_rank("Alice")
print("Alice's Rank:", alice_rank)


class GameLeaderboard:
    def __init__(self):
        self.leaderboard_data = {}

    def add_score(self, player_name, score):
        if player_name in self.leaderboard_data:
            self.leaderboard_data[player_name] += score
        else:
            self.leaderboard_data[player_name] = score

    def get_top_players(self, num_players):
        sorted_leaderboard = sorted(self.leaderboard_data.items(), key=lambda x: x[1], reverse=True)
        return sorted_leaderboard[:num_players]


# 创建游戏排行榜对象
game_leaderboard = GameLeaderboard()

# 模拟玩家分数
game_leaderboard.add_score("Player1", 100)
game_leaderboard.add_score("Player2", 85)
game_leaderboard.add_score("Player3", 120)
game_leaderboard.add_score("Player4", 95)

# 获取前三名玩家
top_players = game_leaderboard.get_top_players(3)
print("Top 3 Players:", top_players)



